/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.SendSignalAction;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.activity.figure.ActionFigure;
import com.cea.papyrus.diagram.activity.figure.SendSignalActionFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controller of a SendSignalAction
 *
 * @author Gilles Cannenterre
 * @since 29 mars 07
 */
public class SendSignalActionEditPart extends InvocationActionEditPart {

	public SendSignalActionEditPart(GraphNode graphNode,
			ModelManager modelManager) {
		super(graphNode, modelManager);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		// Force initialization of the manager when figure has changed
		manager = null;
		// Try to get figure from ContainerWithSemanticEditPart
		IFigure fig = super.createFigure();
		if (!(fig instanceof IconFigure)) {
			return new SendSignalActionFigure();
		}
		// Else
		return fig;				
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphNodeWithSemanticEditPart#getUmlElement()
	 */
	@Override
	public SendSignalAction getUmlElement() {
		Element e = ((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
		// Case of element import
		if(e instanceof ElementImport){
			return (SendSignalAction)((ElementImport) e).getImportedElement();
		}
		// This is a simple AcceptEventAction
		return (SendSignalAction)e;
	}
	protected void refreshVisuals() {
		Debug.debug(this, "refreshVisuals()", channel);
		if(getUmlElement().getSignal()!=null){
			this.prefix=getUmlElement().getSignal().getLabel();
		}
		else{
			this.prefix="";
		}
		super.refreshVisuals();
		IFigure fig=getFigure();
		if (!(fig instanceof IconFigure)) {
			String name=((ActionFigure)fig).getNameLabel().getText();
			if( getUmlElement().getTarget()!=null){
				name= name+ " to "+getUmlElement().getTarget().getType().getName();
				((ActionFigure)fig).setName(name);
			}
	}
		}
}
